<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$return['setting']=$this->plugin_setting('page_setting');
	$return['setting']['balance_money']=$this->setting['balance_money'];
	$diytitle=array('tagent','tshop','tmyshop','tcenter','tbecome','balance_button','tcommission','tcommission1','commission_total','commission_1','balance_total','balance_1','commission_1_1','commission_0','balance_2','balance_3','balance_pay','tcommission_detail','torder','tdown','tmydown','area_title','commission_area');
	foreach($diytitle as $v){
		$return['setting'][$v]=$return['setting'][$v.'2']?:$return['setting'][$v];
	}
	$data=array();
	$data['commission_total']=$this->sum('commission',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid'],'isxuni'=>0),'money');
	$data['commission_0']=$this->sum('commission',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid'],'status'=>0,'isxuni'=>0),'money');
	//待结算佣金
	$data['commission_1']=$this->sum('commission',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid'],'status'=>1,'isxuni'=>0),'money');
	//可结算佣金
	$params=array('mid'=>$_SESSION['uid'],'status'=>1,'isxuni'=>0);
	if($this->setting['balance_day']){
		//结算天数
		$time=TIMESTAMP-intval($this->setting['balance_day'])*86400;
		$params['complete_time <']=$time;
	}
	$data['commission_1_1']=$this->sum('commission',$params,'money');
	$data['balance_total']=$this->sum('balance',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid']),'money');
	$data['balance_1']=$this->sum('balance',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid'],'status'=>1),'money');
	$data['balance_2']=$this->sum('balance',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid'],'status'=>2),'money');
	$data['balance_3']=$this->sum('balance',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid'],'status'=>3),'money');
	$data['balance_pay']=$this->sum('balance',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid'],'status'=>3),'balance_pay');	
	$data['service_money']=intval($this->setting['balance_per']*$data['commission_1_1'])/100;
	$data['cash_money']=$data['commission_1_1']-$data['service_money'];
	foreach($data as &$row){
		$row=price($row);
	}
	$return['data']=$data;
	if($this->setting['withdrawtype']){
		$withdrawtype=array();
		$wxapppay_types=array();
		$check=strstr($this->setting['withdrawtype'],'credit1');
		if($check){
			$withdrawtype['credit1']=$this->pay_types['credit1'];
			$wxapppay_types[]=array(
				'key'=>'credit1',
				'title'=>$this->pay_types['credit1'],
			);
			$pay_type='credit1';
		}
		$check=strstr($this->setting['withdrawtype'],'credit');
		if($check){
			$withdrawtype['credit']=$this->pay_types['credit'];
			$wxapppay_types[]=array(
				'key'=>'credit',
				'title'=>$this->pay_types['credit'],
			);
			$pay_type='credit';
		}
		$check=strstr($this->setting['withdrawtype'],'wechat');
		if($check){
			$withdrawtype['wechat']=$this->pay_types['wechat'];
			$wxapppay_types[]=array(
				'key'=>'wechat',
				'title'=>$this->pay_types['wechat'],
			);
			$pay_type=$pay_type?:'wechat';
		}
		$check=strstr($this->setting['withdrawtype'],'alipay');
		if($check){
			$withdrawtype['alipay']=$this->pay_types['alipay'];
			$wxapppay_types[]=array(
				'key'=>'alipay',
				'title'=>$this->pay_types['alipay'],
			);
			$pay_type=$pay_type?:'alipay';
		}
		$check=strstr($this->setting['withdrawtype'],'union');
		if($check){
			$withdrawtype['union']=$this->pay_types['union'];
			$wxapppay_types[]=array(
				'key'=>'union',
				'title'=>$this->pay_types['union'],
			);
			$pay_type=$pay_type?:'union';
		}
		$check=strstr($this->setting['withdrawtype'],'downline');
		if($check){
			$withdrawtype['downline']=$this->pay_types['downline'];
			$wxapppay_types[]=array(
				'key'=>'downline',
				'title'=>$this->pay_types['downline'],
			);
			$pay_type=$pay_type?:'downline';
		}
		$return['pay_types']=$withdrawtype;
		$return['wxapppay_types']=$wxapppay_types;
	}else{
		$return['pay_types']=$this->pay_types;
		$pay_type='credit1';
	}
	$return['post']=array('pay_type'=>$pay_type);
	set_html('title',$return['setting']['tcommission1']?:'分销佣金');
	exi($return);
}
//结算
if($_RQ['op']=='balance'){
	$post=get_postdata();
	$this->model('commission')->create_balance($_SESSION['uid'],$post);
	exi('申请结算成功！','','main');
}
?>